package com.lw.sbdemo2.service;

import com.github.pagehelper.PageHelper;
import com.lw.sbdemo2.common.base.CrudService;
import com.lw.sbdemo2.common.exception.ApiException;
import com.lw.sbdemo2.common.vo.PageInfo;
import com.lw.sbdemo2.entity.TkExamUnitDict;
import com.lw.sbdemo2.entity.TkTermProduct;
import com.lw.sbdemo2.mapper.TkExamUnitDictMapper;
import com.lw.sbdemo2.mapper.TkTermProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>标题: 题库试题单元字典服务 - 业务逻辑层</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2019</p>
 * <p>公司: 山人行工作室</p>
 *
 * @version: 1.0
 * @author: LinWang
 * @date 2019-4-14
 */
@Slf4j
@Service
public class TkExamUnitDictService extends CrudService<TkExamUnitDict, TkExamUnitDictMapper> {

    @Autowired
    private TkTermProductMapper tkTermProductMapper;

	/**
     * 获取用户分页列表
     *
	 * @param tkExamUnitDict 实体入参
     * @return 用户列表
     */
    public List<Map<String , Object>> getTermUnit(TkExamUnitDict tkExamUnitDict) {
        List<Map<String , Object>> resultMap = new ArrayList<>();

        TkTermProduct tkTermProduct = TkTermProduct.builder().build();
        List<TkTermProduct>  tkTermProductArr = tkTermProductMapper.findList(tkTermProduct);
        List<TkExamUnitDict> tkExamUnitDictlist = this.dao.findList(tkExamUnitDict);

        tkExamUnitDictlist.forEach(item ->{
            String examUnitId = item.getExamUnitId();
            String examUnitName = item.getExamUnitName();
            item.setValue(examUnitId);
            item.setLabel(examUnitName);
        });

        Map<String, List<TkExamUnitDict>> groupByterm =
            CollectionUtils.isEmpty(tkExamUnitDictlist) ? null :tkExamUnitDictlist.stream().collect(Collectors.groupingBy(TkExamUnitDict::getTermId));

        /*resultMap = tkTermProductArr.stream().map(item ->{
                groupByterm.entrySet().stream().map(temp -> {
                    String termId = temp.getKey();
                    List<TkExamUnitDict> temp1 = temp.getValue();
                })
             }
            ).collect(Collectors.toList());*/

        for (TkTermProduct temp: tkTermProductArr) {
            Map<String , Object> map = new HashMap(10);
            String termId = temp.getTermId();
            String termName = temp.getTermName();
            map.put("value" , termId);
            map.put("termId" , termId);
            map.put("label" , termName);
            map.put("termName" , termName);
            // for (Map.Entry<String, List<TkExamUnitDict>> m : groupByterm.entrySet()){
            //     String termUnit = m.getKey();
            //     if (Objects.equals(termId , termUnit)) {
            //         List<TkExamUnitDict> tempArr = m.getValue();
            //         map.put("children" ,tempArr);
            //     }
            // }
            List<TkExamUnitDict> tempArr = groupByterm.get(termId);
            map.put("children" ,tempArr);
            resultMap.add(map);
        }

        return resultMap;
    }

    /**
     * 获取用户分页列表
     *
     * @param query    搜索关键词
     * @param pageInfo 分页实体
     * @param tkExamUnitDict 实体入参
     * @return 用户列表
     */
    public PageInfo<TkExamUnitDict> findListPage(String query, PageInfo pageInfo, TkExamUnitDict tkExamUnitDict) {
        PageHelper.startPage(pageInfo);
        List<TkExamUnitDict> tkExamUnitDictlist = this.dao.findListPage(query, tkExamUnitDict);
        return new PageInfo<>(tkExamUnitDictlist);
    }

    /**
     * 校验shopId是否存在
     *
     * @param tkExamUnitDict 分页实体
     * @return 用户列表
     */
    public void checkIsExist(TkExamUnitDict tkExamUnitDict) throws Exception {

        int count = this.dao.checkCode(tkExamUnitDict.getExamUnitId());
        if (count > 0) {
            throw new Exception("试题单元号已存在，请重新命名！");
        }
    }

    /**
     * 校验代码是否存在
     *
     * @param code  代码
     * @return
     * @throws Exception
     */
    public void checkCode(String code) throws Exception {
        int count = this.dao.checkCode(code);
        if (count > 0) {
            throw new ApiException("代码已存在");
        }
    }

    /**
     * 校验名称是否存在
     *
     * @param name  名称
     * @return
     * @throws Exception
     */
    public void checkName(String termId,String name) throws Exception {
        int count = this.dao.checkName(termId,name);
        if (count > 0) {
            throw new ApiException("名称已存在");
        }
    }

    /**
     * 更新校验名称是否存在
     *
     * @param tkExamUnitDict  名称
     * @return
     * @throws Exception
     */
    public void updateCheckName(TkExamUnitDict tkExamUnitDict) throws Exception {
        int count = this.dao.updateCheckName(tkExamUnitDict.getExamUnitId(),
                tkExamUnitDict.getExamUnitName(),tkExamUnitDict.getTermId());
        if (count > 0) {
            throw new ApiException("名称已存在");
        }
    }

}